/*
 * @Author: 米亚流年 miyaliunian@gmail.com
 * @Date: 2023-12-31 15:30:20
 * @LastEditors: 米亚流年 miyaliunian@gmail.com
 * @LastEditTime: 2024-03-27 16:13:32
 * @FilePath: /Component-Gallery/docs/scripts/genDemoRoute.js
 * @Description: 生成路由
 */

const path = require('path')
const globby = require('globby')
const { ESLint } = require('eslint')
const { makeMenu } = require('./utils/make-menu')
const { engine } = require('./utils/eslint-config')
const { writFile } = require('./utils/write-file')
;(async () => {
  const components = {}
  const menu = []
  const modules = await globby(
    [
      'components/*/demo/index.*.md',
      'components/common-base-components/*/demo/index.*.md'
    ],
    {
      cwd: path.resolve(__dirname, '..', '..', 'packages/')
    }
  )

  modules.forEach(async (module) => {
    const componentName = module.split('/')
    menu.push(
      makeMenu(
        module,
        componentName[1] === 'common-base-components'
          ? componentName[2]
          : componentName[1]
      )
    )
    const demoIndex = module
      .split('/')
      .findIndex((part) => part.includes('demo'))
    // components[componentName[demoIndex - 1]] = componentName[demoIndex - 1]
    components[componentName[demoIndex - 1]] = module
  })
  const TEMPLATE = `
  export default  [
    ${Object.keys(components).map(
      (component) => `
    {
      path: '${component}',
      component: () =>  import('../../../packages/${components[component]}'),
    }`
    )}
  ];`
  const routeOutput = `${__dirname}/../demo/routes/apiRoute.ts`
  await writFile(routeOutput, TEMPLATE)
  const menuTemplate = `export default ${JSON.stringify(menu, null, 2)}`
  const menuOutPut = path.resolve(__dirname, '..', 'demo', 'utils', 'menus.ts')
  await writFile(menuOutPut, menuTemplate)
})()
